﻿/****************************************************************
 * Build by hong 2011.9
 *
 * SET begin
 ***************************************************************/

function SET(data) {
  var set = this;
  set.setArray = [];

  set.SET_SIZE = function () {
    return set.setArray.length;
  };

  function findIndex(node) {
    var i = 0, len = set.SET_SIZE();

    for (i = 0; i < len; i += 1) {
      if (set.setArray[i] === node) {
        return i;
      }
    }
    return -1;
  }


  set.SET_POP = function () {
    return set.setArray.pop();
  };

  set.SET_PUSH = function (node) {
    var flag = findIndex(node);
    if (flag === -1) {
      set.setArray.push(node);
    }
  };

  set.SET_PUSH_ARRAY = function (arr) {
    var i = 0, len = arr.length, flag;

    for (i = 0; i < len; i += 1) {
      flag = findIndex(arr[i]);
      if (flag === -1) {
        set.setArray.push(arr[i]);
      }
    }
  };

  set.MOVEOUT = function (node) {
    var index = findIndex(node);
    if (index !== -1) {
      set.setArray.splice(index, 1);
    }
  };

  set.MOVEOUTARRAY = function (arr) {
    var i = 0, len = arr.length;
    for (i = 0; i < len; i += 1) {
      set.MOVEOUT(arr[i]);
    }
    return set.setArray;
  };

  set.SORT = function () {
    set.setArray.sort();
  };

  set.GETARRAY = function () {
    return set.setArray;
  };

  set.PRINT = function () {
    alert(set.setArray.toSource());
  };

  function INIT() {
    if (data instanceof Array) {
      set.setArray = data;
    }
  }

  INIT();
}